package com.zukxu.items.common;

import java.io.File;
import java.util.Objects;

/**
 * <p>
 * 清除未下载的maven包
 * </p>
 *
 * @author zukxu
 * @date 2020/12/25 0025 11:09
 */
public class CleanMaven {
	/**
	 * maven仓库地址
	 */
	private static final String MAVEN_REPO_PATH = "D:\\Repository";
	/**
	 * 文件后缀
	 */

	private static final String[] SUFFIX = {".lastUpdated", "jar-in-progress"};

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		File mavenRep = new File(MAVEN_REPO_PATH);
		if (!mavenRep.exists()) {
			System.out.println("Maven repos is not exist.");
			return;
		}
		checkAndDeleteFiles(Objects.requireNonNull(mavenRep.listFiles()));
		System.out.println("Clean lastUpdated files and in-progess jar finished.");
	}

	private static boolean checkAndDeleteFiles(File[] files) {
		for (File file : files) {
			if (file.isDirectory()) {
				if (Objects.requireNonNull(file.listFiles()).length == 0) {
					file.delete();
				} else {
					boolean flag = checkAndDeleteFiles(Objects.requireNonNull(file.listFiles()));
					if (flag) {
						for (File childFile : Objects.requireNonNull(file.listFiles())) {
							childFile.delete();
						}
						System.out.println(file.getAbsolutePath());
						file.delete();
					}
				}
			} else {
				for (int i = 0; i < SUFFIX.length; i++) {
					if (file.getName().contains(SUFFIX[i])) {
						System.out.println(file.getName());
						return true;
					}
				}
			}
		}
		return false;
	}

}